package com.lenovo.leos.filebrowser.engine.operation;

import java.io.File;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.StatFs;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.leos.filebrowser.ui.BaseActivity;
import com.lenovo.leos.filebrowser.R;

import com.lenovo.leos.filebrowser.engine.util.FileUtil;
import com.lenovo.leos.filebrowser.engine.util.Log;

public class FileNew {
	private static final String TAG = "FileNew";
	
	public static final int MSG_RES_NEWDIR_OK = R.string.msg_newdir_res_ok;
	public static final int MSG_RES_NEWDIR_FAILED = R.string.msg_newdir_res_failed;
	
	public static final int NEW_FILE_DOC = 0;
	public static final int NEW_FILE_PPT = 1;
	public static final int NEW_FILE_XLS = 2;
	
	private static BaseActivity sActivity = null; 
	
	public static void createFolder(BaseActivity activity, File parent){
		Log.i(TAG, "==> createFolder");

		if (null == activity 
				|| null == parent || !parent.exists() || !parent.isDirectory()) {
			Log.e(TAG, "invalid parameter in renameFile");
			return;
		}else {
			sActivity = activity;
			checkThenCreateFolder(parent);
		}
	}
	
	private static void checkThenCreateFolder(final File parent){
		
		LayoutInflater inflater = LayoutInflater.from(sActivity);

		final View content = inflater.inflate(R.layout.dlg_content_newname, null);
		
		final TextView tv = (TextView) content.findViewById(R.id.filename_view);
		tv.setText(R.string.dlg_newfolder_msg_input);

		final EditText et = (EditText) content.findViewById(R.id.filename_edit);
		String name = FileUtil.getValidFolderName(parent, 
				sActivity.getResources().getString(R.string.default_dir_name));
		et.setText(name);
		et.setFilters(new InputFilter[] { new InputFilter.LengthFilter(FileUtil.FILE_NAME_NUM_MAX) });
		et.selectAll();
		
		final Runnable reOpen = new Runnable() {
//			@Override
			public void run() {
				Log.i(TAG, "==> run");
				// TODO Auto-generated method stub
				checkThenCreateFolder(parent);
			}
		};
		
		new AlertDialog.Builder(sActivity)
		.setTitle(R.string.dlg_newfolder_title)
		.setView(content)
		.setPositiveButton(R.string.alert_dialog_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						String name = et.getText().toString().trim();
						if (name.length() > 0 && name.length() <= FileUtil.FILE_NAME_NUM_MAX) {
							if (FileUtil.isNameUseable(name, true) == false) {
								Toast.makeText(sActivity,R.string.namebad, Toast.LENGTH_SHORT)
										.show();
								sActivity.postRunnable(reOpen);
							} else {
								File f = new File(parent.getPath() + "/"+ name);
								if(f.exists()){
									Log.i(TAG, "the file is existed");
								}
								if (f.exists() && f.isDirectory()) {
									Toast.makeText(sActivity,R.string.nameexist, Toast.LENGTH_SHORT).show();
									sActivity.postRunnable(reOpen);
								} else if (FileUtil.isSysOrReservedDir(f)) {
									Toast.makeText(sActivity, f.getName() 
											+ sActivity.getResources().getString(R.string.sysReservedDir),
											Toast.LENGTH_SHORT).show();
									sActivity.postRunnable(reOpen);
								} else {
									Log.i(TAG, "before create new folder");
									// create the new folder
									StatFs sf;
									sf = new StatFs(FileUtil.sdroot); 
									if (((long) sf.getAvailableBlocks() * sf.getBlockSize()) < (long) 10 * 1024) {
										//no enough space
										new AlertDialog.Builder(sActivity)
										.setTitle(R.string.remind)
										.setMessage(R.string.nospaceonsd).
										setPositiveButton(R.string.alert_dialog_ok,
												new DialogInterface.OnClickListener() {
													public void onClick(DialogInterface dialog,
															int whichButton) {
													}})
										.show();
									} else {
										createDir(parent, name);	
									}
								}
							}
						} else if (name.length() > FileUtil.FILE_NAME_NUM_MAX) {
							Toast.makeText(sActivity,
									R.string.name_too_long, Toast.LENGTH_SHORT)
									.show();
							sActivity.postRunnable(reOpen);
						} else {
							Toast.makeText(sActivity,
									R.string.nameempty, Toast.LENGTH_SHORT)
									.show();
							sActivity.postRunnable(reOpen);
						}
					}
				})
				.setNegativeButton(R.string.alert_dialog_cancel,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// User clicked cancel
						// so do some stuff
					}
				})
				.show();
	}
	
	private static void createDir(File parent, String name) {
		Log.i(TAG, "==> createDir");

		File dir = new File(parent.getAbsolutePath() + "/" + name);

		if (dir.exists() && dir.isDirectory()) {
			Log.e(TAG, "the new folder is existent");
			return;
		} else {
			if (dir.mkdir()) {
				Toast.makeText(sActivity, R.string.tst_newfolder_msg_ok, 3).show();
			} else {
				Toast.makeText(sActivity, R.string.tst_newfolder_msg_fail, 3).show();
			}

			if (dir.exists()) {
				sActivity.sendEmptyMsg(MSG_RES_NEWDIR_OK);
			} else {
				sActivity.sendEmptyMsg(MSG_RES_NEWDIR_FAILED);
			}
		}
	}	
	
	public static void createFile(int type){
		
	}
		
}